package com.imu.purse.modules.trade.dao;

import com.imu.core.base.BaseMapper;
import com.imu.purse.modules.trade.entity.TransDetailLogEntity;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * IMU转账上链详情表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-07-01 16:21:08
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
public interface TransDetailLogMapper extends BaseMapper<TransDetailLogEntity>
{
	/**
     * 分页查询
     * @return
     */
	@Select({
		"<script>",
		    "SELECT id,",
					"log_id, ",
					"pay_addr, ",
					"red_addr, ",
					"trans_id, ",
					"money, ",
					"fee, ",
					"create_time, ",
					"update_time",
		    "FROM imu_trans_detail_log ",
		    "WHERE 1 = 1",
			"<when test = 'logId != null'>",
		    	"AND `log_id` = #{logId}",
		    "</when>",
			"<when test = 'payAddr != null'>",
		    	"AND `pay_addr` = #{payAddr}",
		    "</when>",
			"<when test = 'redAddr != null'>",
		    	"AND `red_addr` = #{redAddr}",
		    "</when>",
			"<when test = 'transId != null'>",
		    	"AND `trans_id` = #{transId}",
		    "</when>",
			"<when test = 'money != null'>",
		    	"AND `money` = #{money}",
		    "</when>",
			"<when test = 'fee != null'>",
		    	"AND `fee` = #{fee}",
		    "</when>",
			"<when test = 'createTime != null'>",
		    	"AND `create_time` = #{createTime}",
		    "</when>",
			"<when test = 'updateTime != null'>",
		    	"AND `update_time` = #{updateTime}",
		    "</when>",
		    "<when test = 'stimeStr != null'>",
		    	"AND cs &gt;= #{stimeStr}",
		    "</when>",
		    "<when test = 'etimeStr != null'>",
				"AND cs &lt;= #{etimeStr}",
			"</when>",
		    "ORDER BY cs DESC",
    	"</script>"
	})
    List<TransDetailLogEntity> findByPage(final TransDetailLogEntity entity);

	/**
	 * 根据交易ID和时间获取数据
	 * @param entity
	 * @return
	 */
	@Select(
		{
			"select id,log_id,money,fee from imu_trans_detail_log "
				+ " where trans_id = #{transId} "
				+ " and YEAR(create_time) = YEAR(FROM_UNIXTIME(#{transTime}))"
		}
	)
	TransDetailLogEntity findByTId(final TransDetailLogEntity entity);

	/**
	 * 根据交易ID和时间修改shuju
	 * @param entity
	 * @return
	 */
	@Update({
		"update imu_trans_detail_log set fee = money - #{money},update_time = #{updateTime} " +
				" where trans_id = #{transId} "
				+ " and YEAR(create_time) = YEAR(FROM_UNIXTIME(#{transTime}))"
	})
	int moByTId(final TransDetailLogEntity entity);
}
